package cn.sunxyz.module.honey.domain;

import cn.sunxyz.common.site.domain.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.util.Set;

/**
 * Created by sunxyz on 2017/3/2.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "new", "del", "parent", "sites", "selfPathName"})
@Entity(name = "folder")
public class FolderDO extends BaseEntity{

    private String name;

    private String description;

    @ManyToOne(fetch = FetchType.LAZY)
    private FolderDO parent;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(
            name = "folder_site",
            joinColumns = @JoinColumn(name = "folder_id"),
            inverseJoinColumns = @JoinColumn(name = "site_id")
    )
    private Set<SiteDO> sites;

    @Formula("(select count(*) from folder f where f.parent_id=id)")
    @Transient
    private Boolean hasChildren;

    private String selfPathName;

    private String fullPath;//完整路径

    {
        hasChildren = false;
    }

    public String getName() {
        return name;
    }

    public FolderDO setName(String name) {
        this.name = name;
        return this;
    }

    public String getDescription() {
        return description;
    }

    public FolderDO setDescription(String description) {
        this.description = description;
        return this;
    }

    public FolderDO getParent() {
        return parent;
    }

    public FolderDO setParent(FolderDO parent) {
        this.parent = parent;
        return this;
    }

    public Set<SiteDO> getSites() {
        return sites;
    }

    public FolderDO setSites(Set<SiteDO> sites) {
        this.sites = sites;
        return this;
    }

    public Boolean getHasChildren() {
        return hasChildren;
    }

    public FolderDO setHasChildren(Boolean hasChildren) {
        this.hasChildren = hasChildren;
        return this;
    }

    public String getSelfPathName() {
        return selfPathName;
    }

    public FolderDO setSelfPathName(String selfPathName) {
        this.selfPathName = selfPathName;
        return this;
    }

    public String getFullPath() {
        return fullPath;
    }

    public FolderDO setFullPath(String fullPath) {
        this.fullPath = fullPath;
        return this;
    }
}
